#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>

#include <QVTKOpenGLNativeWidget.h>
#include <vtkActor.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkDiskSource.h>
#include <vtkProperty.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkVectorText.h>
#include <vtkFollower.h>
#include <vtkPolyDataMapper.h>
#include <vtkTextProperty.h>
#include <vtkTransform.h>
#include <vtkLineSource.h>  // 添加线条源
#include <vtkPolyData.h>
#include <vtkPoints.h>
#include <vtkCellArray.h>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // 1. 创建主窗口和布局
    QWidget mainWindow;
    QHBoxLayout *layout = new QHBoxLayout(&mainWindow);
    mainWindow.setWindowTitle("VTK + Qt 5 平面圆环示例 (VTK 9.3)");

    // 2. 创建QVTKOpenGLNativeWidget
    QVTKOpenGLNativeWidget *vtkWidget = new QVTKOpenGLNativeWidget();
    layout->addWidget(vtkWidget);

    // 3. 创建并设置VTK渲染窗口
    vtkSmartPointer<vtkGenericOpenGLRenderWindow> renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
    vtkWidget->setRenderWindow(renderWindow);

    // 4. 创建圆环几何数据
    vtkSmartPointer<vtkDiskSource> disk = vtkSmartPointer<vtkDiskSource>::New();
    disk->SetInnerRadius(4.0);
    disk->SetOuterRadius(5.0);
    disk->SetRadialResolution(1);
    disk->SetCircumferentialResolution(50);

    // 5. 创建映射器和演员
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(disk->GetOutputPort());

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor(0.2, 0.6, 1.0);
    actor->GetProperty()->SetLineWidth(2);

    // 6. 创建渲染器
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->SetBackground(0.8, 0.8, 0.8);

    // 7. 计算圆环参数
    double innerRadius = disk->GetInnerRadius();
    double outerRadius = disk->GetOuterRadius();
    double ringWidth = outerRadius - innerRadius;
    double middleRadius = (innerRadius + outerRadius) / 2.0;
    double textHeight = ringWidth * 0.5;

    // 8. 创建四个方向的文字
    const char *directions[] = {"E", "S", "W", "N"};

    for (int i = 0; i < 4; ++i)
    {
        vtkSmartPointer<vtkVectorText> textSource = vtkSmartPointer<vtkVectorText>::New();
        textSource->SetText(directions[i]);

        vtkSmartPointer<vtkPolyDataMapper> textMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        textMapper->SetInputConnection(textSource->GetOutputPort());

        vtkSmartPointer<vtkActor> textActor = vtkSmartPointer<vtkActor>::New();
        textActor->SetMapper(textMapper);
        textActor->SetScale(textHeight);

        double position[3] = {0, 0, 0.001};
        double offset = textHeight * 0.4;

        if (i == 0)
        {  // E - 东
            position[0] = middleRadius - offset;
            position[1] = 0;
        }
        else if (i == 1)
        {  // S - 南
            position[0] = -textHeight * 0.5;
            position[1] = -middleRadius - offset;
        }
        else if (i == 2)
        {  // W - 西
            position[0] = -middleRadius - textHeight;
            position[1] = 0;
        }
        else if (i == 3)
        {  // N - 北
            position[0] = -textHeight * 0.5;
            position[1] = middleRadius - offset;
        }

        textActor->SetPosition(position);
        textActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
        renderer->AddActor(textActor);
    }

    // 9. 在四个斜角方向添加分割线
    const double angle45 = 45.0 * 3.141592653589793 / 180.0;  // 45度对应的弧度
    const double cos45 = cos(angle45);
    const double sin45 = sin(angle45);

    // 四个斜角方向：东北(45°)、东南(135°)、西南(225°)、西北(315°)
    double angles[4] = {45.0, 135.0, 225.0, 315.0};

    for (int i = 0; i < 4; ++i)
    {
        double angle = angles[i] * 3.141592653589793 / 180.0;
        double cosAngle = cos(angle);
        double sinAngle = sin(angle);

        // 计算分割线的起点和终点
        // 起点在圆环内半径，终点在圆环外半径
        double startPoint[3] = {innerRadius * cosAngle, innerRadius * sinAngle, 0.002};
        double endPoint[3] = {outerRadius * cosAngle, outerRadius * sinAngle, 0.002};

        // 创建线条
        vtkSmartPointer<vtkLineSource> lineSource = vtkSmartPointer<vtkLineSource>::New();
        lineSource->SetPoint1(startPoint);
        lineSource->SetPoint2(endPoint);
        lineSource->Update();

        // 创建线条映射器和演员
        vtkSmartPointer<vtkPolyDataMapper> lineMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
        lineMapper->SetInputConnection(lineSource->GetOutputPort());

        vtkSmartPointer<vtkActor> lineActor = vtkSmartPointer<vtkActor>::New();
        lineActor->SetMapper(lineMapper);
        lineActor->GetProperty()->SetColor(1.0, 0.0, 0.0);  // 分割线颜色
        lineActor->GetProperty()->SetLineWidth(2);          // 设置线宽

        renderer->AddActor(lineActor);
    }

    // 10. 将渲染器添加到渲染窗口
    renderWindow->AddRenderer(renderer);

    // 11. 重置相机以确保所有对象可见
    renderer->ResetCamera();

    // 12. 设置交互器样式
    vtkSmartPointer<vtkInteractorStyleTrackballCamera> style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
    renderWindow->GetInteractor()->SetInteractorStyle(style);

    // 13. 显示窗口并启动应用
    mainWindow.resize(800, 600);
    mainWindow.show();

    return app.exec();
}

